import asyncio
import numpy as np
from multiprocessing import Process, Pipe
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pyqtgraph as pg
import time
from PyQt5.QtWidgets import QSizePolicy

class MainGui(QtWidgets.QMainWindow):
    left = 100
    top = 100
    width = 960
    height = 700
    length = 400
    output_heart=[0 for j in range(0, length)]
    def __init__(self):
        super().__init__()
        self.title = 'Contoller'
        self.acc = 0
        self.init_ui()
    def init_ui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.main_widget = QtWidgets.QWidget() 
        self.main_layout = QtWidgets.QGridLayout()
        self.main_widget.setLayout(self.main_layout)

        self.left_widget = QtWidgets.QWidget()
        self.left_layout = QtWidgets.QGridLayout()
        self.left_widget.setLayout(self.left_layout)
        self.left_widget.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)

        self.right_widget = QtWidgets.QWidget()
        self.right_layout = QtWidgets.QGridLayout()
        self.right_widget.setLayout(self.right_layout)
        self.right_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        self.main_layout.addWidget(self.left_widget, 0, 0, 13, 1)
        self.main_layout.addWidget(self.right_widget, 0, 1, 12, 6)

        self.main_layout.setColumnStretch(0, 1)
        self.main_layout.setColumnStretch(1, 6)
        self.setCentralWidget(self.main_widget)  # 设置窗口主部件

        self.lb1 = QtWidgets.QLabel("温度检测:24.125")
        self.lb2 = QtWidgets.QLabel("心跳速率:0")
        self.lb3 = QtWidgets.QLabel("步数计算:0")
        self.lb4 = QtWidgets.QLabel("里程计算:0")
        btn1 = QtWidgets.QPushButton("里程清零")
        btn1.clicked.connect(self.cle)

        plot_Rate_heart = pg.PlotWidget()
        self.autoPixelRange=True

        self.left_layout.addWidget(self.lb1, 0, 0, 1, 2)
        self.left_layout.addWidget(self.lb2, 2, 0, 1, 2)
        self.left_layout.addWidget(self.lb3, 4, 0, 1, 2)
        self.left_layout.addWidget(self.lb4, 6, 0, 1, 2)
        self.left_layout.addWidget(btn1, 7, 0)
        self.left_layout.setRowStretch(0, 1)
        self.left_layout.setRowStretch(1, 1)
        self.left_layout.setRowStretch(2, 1)
        self.left_layout.setRowStretch(3, 1)
        self.left_layout.setRowStretch(4, 1)
        self.left_layout.setRowStretch(5, 1)
        self.left_layout.setRowStretch(6, 1)
        self.left_layout.setRowStretch(7, 1)
        self.left_layout.setRowStretch(8, 7)

        self.right_layout.addWidget(plot_Rate_heart, 1, 0, 3, 2)
        self.right_layout.setRowStretch(1, 0)
        self.right_layout.setRowStretch(2, 5)
        self.right_layout.setRowStretch(11, 0)
        
        self.plot_Rate_heart = plot_Rate_heart.plot()
        self.plot_Rate_heart.setData(self.output_heart,
                                     pen='y', symbol=None, symbolBrush='g')
    def refresh(self, data):
        t = data["Temp"]
        if t:
            self.lb1.setText('温度检测:%f' % t)
            QtWidgets.QApplication.processEvents()
        t = data["Step"]
        if t:
            self.acc += t[1]
            self.lb3.setText('步数计算:%d' % t[0])
            self.lb4.setText('里程计算:%d' % (self.acc + t[0]) * 0.75)
            QtWidgets.QApplication.processEvents()
        t = data["ECG"]
        if t:
            self.lb2.setText('心跳速率:%f' % t[-1][1])
            QtWidgets.QApplication.processEvents()
            self.output_heart = (self.output_heart + [i[0] for i in t])[-400:]
            self.plot_Rate_heart.setData(self.output_heart,
                                     pen='y', symbol=None, symbolBrush='g')
    def cle(self):
        self.acc = 0

conn_send, conn_recv = Pipe()
app = QtWidgets.QApplication(sys.argv)
gui = MainGui()
gui.show()
gui_handle = app.exec_()
while(True):
    gui.refresh(conn_recv.recv())
sys.exit(gui_handle)